package com.sign2024.sign2024project.projectservice.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sign2024.sign2024core.common.constants.CommonConstants;
import com.sign2024.sign2024core.common.exeception.CommonException;
import com.sign2024.sign2024core.common.utils.StringUtils;
import com.sign2024.sign2024core.entry.projectservice.BatchNeed;
import com.sign2024.sign2024core.entry.projectservice.RecordCollect;
import com.sign2024.sign2024core.entry.projectservice.RecordCollectTemp;
import com.sign2024.sign2024project.projectservice.mapper.RecordCollectMapper;
import com.sign2024.sign2024project.projectservice.param.BatchNeedParam;
import com.sign2024.sign2024project.projectservice.param.RecordCollectParam;
import com.sign2024.sign2024project.projectservice.service.RecordCollectService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 获取记录汇总 业务层
 *
 * @author yujie
 * @data 2024/7/26 10:30
 */
@Service
public class RecordCollectServiceImpl implements RecordCollectService {

    @Resource
    private RecordCollectMapper recordCollectMapper;


    @Override
    public Page<RecordCollectTemp> pageRecordCollectTemp(RecordCollectParam param) {
        Integer offset = (param.getPageNum() - 1) * param.getPageSize();
        param.setOffset(offset);

        // 分页查询
        Page<RecordCollectTemp> page = new Page<>(param.getPageNum(), param.getPageSize());
        List<RecordCollectTemp> collectTempList = recordCollectMapper.selectRecordCollectByPage(param);

        // 页码
        page.setCurrent(param.getPageNum());
        page.setSize(param.getPageSize());

        // 总记录数
        Long aLong = recordCollectMapper.pageRecordCollectCount(param);
        page.setTotal(aLong);
        page.setRecords(collectTempList);

        return page;
    }

    @Override
    public void excelImportRecordCollectTemp(List<RecordCollectTemp> list, Integer taskId) {
        if (StringUtils.isEmpty(list)) {
            throw new CommonException(CommonConstants.ERROR, "excel数据导入失败");
        } else {
            // 批量新增
            recordCollectMapper.insertRecordCollectTemp(list, taskId);
        }
    }

    @Override
    public boolean deleteTempById(Integer taskId) {
        int total = recordCollectMapper.deleteTempById(taskId);
        return total > 0;
    }

    @Override
    public List<RecordCollect> countRecordCollect(Integer taskId) {
        List<RecordCollect> list = recordCollectMapper.countRecordCollect(taskId);
        return list;
    }

    @Override
    public void dealWithRecordCollect(Integer taskId) {
        List<RecordCollect> list = recordCollectMapper.countRecordCollect(taskId);
        if (StringUtils.isNotEmpty(list)) {
            recordCollectMapper.insertRecordCollect(list, taskId);
        }
    }

    @Override
    public void deleteRecordCollectByTaskId(Integer taskId) {
        recordCollectMapper.deleteRecordCollectByTaskId(taskId);
    }
}
